The user runs the program and rates hunger, appearance, and aroma as 4.5, 6.2, and 9.9, respectively. What will the program print?
How hungry are you (1-10): 4.5 How nice does the cookie look (1-10): 6.2 How nice does the cookie smell (1-10): 9.9 Buy the cookie! Continue down the Mall.
The true-branch is executed because the sum of the ratings, 20.6, was greater than the threshold of 15.0:
(hunger + look + smell ) > 15.0
is true.
This boolean expression is more complicated than previous ones. The values of the variables hunger, look, and smell are added up, and it is the sum that is compared to the threshold, 15.0. Boolean expressions can get very complicated, and operator precedence rules will be needed to figure them out. This topic will be discussed in a later chapter. For now, remember that:
Relational operators have lower precedence than arithmetic operators.
The above Boolean expression could be written as:
hunger + look + smell > 15.0
Since all the +
are of higher precedence than the >
the
+
will operate first.